<?php
namespace Common\Models;
use Common\BaseModel;
class  OrderCommissions extends BaseModel
{
	public $res;
	public $key;
	public function getSource()
    {
        return "w_order_commissions";
    }

	public function initialize()
	{
		 parent::initialize();
	}


    /**
     * @func 未结算的求和
     * @param $user_id 用户id
     * @return string 返回没有结算的余额
     */
	public function sum_money($user_id){
        $types = [4,6];
        $sum_money = $this::sum(array(
            "conditions" => 'user_id='.$user_id.' and order_status IN (2,4,6) and settlement_type=0',
            "column" => "money+money_plus",
            "bind" => array(
                'typeList'=>$types,
            )
        ));
        return $sum_money ? $sum_money : '0.00';
    }



	//总预估
	public function all_pre($user_id){
		$condition=["conditions" => 'user_id='.$user_id.' and order_status IN (2,4,6) ',
			"columns" => "sum(money+money_plus) as money"];
		$res=$this->findFirst($condition);
		return $res->money.'';
	
	}



	//总预估没有确认收货的
	public function all_pre_no_goods($user_id){
		$condition=["conditions" => 'user_id='.$user_id.' and order_status IN (2,6) ',
				"columns" => "sum(money+money_plus) as money"];
		$res=$this->findFirst($condition);
		return $res->money.'';

	}


	//总订单
	public function order_all($user_id,$start_end_time=0,$data=[]){
		if($start_end_time){
		    $conditions = "user_id={$user_id} and order_status IN (2,4,6)  AND tk_create_time >= '{$start_end_time['start_at']}' AND tk_create_time < '{$start_end_time['end_at']}' and  (money + money_plus) > 0 ";
		}else{
		    $conditions = 'user_id='.$user_id.' and order_status IN (2,4,6) and  (money + money_plus) > 0';
		}
		if(isset($data['buyer_id']) && !empty($data['buyer_id'])){
		    if($conditions){
                $conditions .= " AND buyer_id={$data['buyer_id']}";
            }else{
                $conditions = "buyer_id={$data['buyer_id']}";
            }
        }
		$res=$this->count(array(
            $conditions,
            "columns" => "id"
        ));

        return $res.'';
	
	}


	/*
	 算用户已经结算的用户的转的钱，主要用于用户升高创
	 */
	public function sum_money_up($user_id){
		$condition=[
		    "conditions" => 'user_id='.$user_id.' and order_status =6',
			"column" => "sum(money+money_plus) as money"
        ];
		$res=$this->findFirst($condition);
		if($res){
            return $res->money;
        }else{
		    return '0.00';
        }
	}

    /**
     * @func 获取某时间段粉丝订单数量 自购订单数量 以前的
     * @param $user_id 用户id
     * @param $start_end_time 开始时间和结束时间
     * @param $site 所属平台 taobao pinduoduo jd 如果没有，则查找三个平台的总和
     * @return array 以平台名称作为键 统计作为结果
     */
    public function getFansOrderCount($user_id,$start_end_time,$site=''){
        if($site){
            $conditions = "user_id = ?1 AND tk_create_time >= ?2 AND tk_create_time < ?3 AND site = ?4 AND commission_type=2";
            $parameters = array(1 => $user_id,2 => $start_end_time['start_at'],3 => $start_end_time['end_at'],4=>$site);
        }else{
            $conditions = "user_id = ?1 AND tk_create_time >= ?2 AND tk_create_time < ?3 AND commission_type=2";
            $parameters = array(1 => $user_id,2 => $start_end_time['start_at'],3 => $start_end_time['end_at']);
        }
        $count_array = $this :: count(array(
            $conditions,
            "column" => "*",
            "group" => "site",
            "bind" => $parameters
        ));
        $new_array = array();
        foreach($count_array as $key=>$value){
            $new_array[$value['site']] = $value['rowcount'];
        }
        return $new_array;
    }




    public function getFansOrderCounts($user_id,$start_end_time,$site=''){
        if($site){
            $conditions = "oc.user_id = {$user_id} and oc.commission_type=2 and  oc.order_status in (2,4,6)";
        }else{
            $conditions = "oc.user_id = {$user_id} and oc.commission_type=2 and oc.tk_create_time>='{$start_end_time['start_at']}' and oc.tk_create_time<='{$start_end_time['end_at']}' and oc.order_status in (2,4,6)";
        }
        $phql = "
        SELECT count(oc.id) as rowcount,oc.site FROM w_order_commissions as oc
        LEFT JOIN `w_union_order` as o ON o.order_id = oc.order_id
        where  ".$conditions ." group by oc.site";
        $rows = $this->getDI()->get('dbMaster')->fetchAll($phql);
        foreach($rows as $key=>$value){
            $new_array[$value['site']] = $value['rowcount'];
        }
        return $new_array;
    }





    /**
     * 用户付款预估收益（自购+间接）
     * @param $user_id 用户id
     * @param $start_end_time 开始时间和结束时间
     * @param $site 所属平台 taobao pinduoduo jd 如果没有，则查找三个平台的总和
     */
    public function getOrderChannelEstimateCommission($user_id,$start_end_time,$site="",$data=[]){
        if($site){
            $conditions = "user_id = {$user_id} AND tk_create_time >= '{$start_end_time['start_at']}' AND tk_create_time < '{$start_end_time['end_at']}' AND order_status in (2,4,6) AND site = {$site}";
        }else{
            $conditions = "user_id = {$user_id} AND tk_create_time >= '{$start_end_time['start_at']}' AND tk_create_time < '{$start_end_time['end_at']}' AND order_status in (2,4,6)";
        }

        if(isset($data['buyer_id'])){
            $conditions .= " AND buyer_id in({$data['buyer_id']})";
        }
        if(isset($data['nobuyer_id'])){
            $conditions .= " AND buyer_id<>{$data['nobuyer_id']}";
        }

        $money_array = $this :: SUM(array(
            $conditions,
            "column" => "money+money_plus",
            "group" => "site",
        ));
        $new_array = array();
        foreach($money_array as $key=>$value){
            $new_array[$value['site']] = $value['sumatory'];
        }
        return $new_array;
    }









    /**
     * 待结算（自购+间接）
     * @param $user_id 用户id
     * @param $start_end_time 开始时间和结束时间
     * @param $site 所属平台 taobao pinduoduo jd 如果没有，则查找三个平台的总和
     */
    public function getOrderChannelEstimateCommission_daijie($user_id,$start_end_time,$site="",$data=[]){
        if($site){
            $conditions = "user_id = {$user_id} AND tk_create_time >= '{$start_end_time['start_at']}' AND tk_create_time < '{$start_end_time['end_at']}' AND order_status in (2,4) AND site = {$site}";
        }else{
            $conditions = "user_id = {$user_id} AND tk_create_time >= '{$start_end_time['start_at']}' AND tk_create_time < '{$start_end_time['end_at']}' AND order_status in (2,4)";
        }


        if(isset($data['buyer_id'])){
            $conditions .= " AND buyer_id in({$data['buyer_id']})";
        }

        $money_array = $this :: SUM(array(
            $conditions,
            "column" => "money+money_plus",
            "group" => "site",
        ));
        $new_array = array();
        foreach($money_array as $key=>$value){
            $new_array[$value['site']] = $value['sumatory'];
        }
        return $new_array;
    }



	/**
	 * 用户付款预估收益（自购+间接）
	 * @param $user_id 用户id
	 * @param $start_end_time 开始时间和结束时间
	 * @param $site 所属平台 taobao pinduoduo jd 如果没有，则查找三个平台的总和
	 */
	public function getOrderChannelEstimateCommission_ids($user_id,$start_end_time,$site=""){
		if($site){
			$conditions = "user_id in(?1) AND tk_create_time >= ?2 AND tk_create_time < ?3 AND order_status in (2,4,6) AND site = ?4";
			$parameters = array(1 => $user_id,2 => $start_end_time['start_at'],3 => $start_end_time['end_at'],4=>$site);
		}else{
			$conditions = "user_id in(?1) AND tk_create_time >= ?2 AND tk_create_time < ?3 AND order_status in (2,4,6)";
			$parameters = array(1 => $user_id,2 => $start_end_time['start_at'],3 => $start_end_time['end_at']);
		}
		$money_array = $this :: SUM(array(
				$conditions,
				"column" => "money+money_plus",
				"group" => "site",
				"bind" => $parameters
		));

        $new_array = array();
		foreach($money_array as $key=>$value){
			$new_array[$value['site']] = $value['sumatory'];
		}

        return $new_array;
	}


    /**
     * 用户付款预估收益（自购 / 间接）分开
     * @param $user_id 用户id
     * @param $start_end_time 开始时间和结束时间
     * @param $site 所属平台 taobao pinduoduo jd 如果没有，则查找三个平台的总和
     */
    public function getOrderChannelEstimateCommission_list($user_id){
        $conditions = "user_id in(?1) AND  order_status in (2,4,6) and commission_type = 1";
        $parameters = array(1 => $user_id);
        $this_money = $this :: SUM(array(
            $conditions,
            "column" => "money+money_plus",
            "bind" => $parameters
        ));
        $conditions = "user_id in(?1)  AND order_status in (2,4,6) and commission_type = 2";
        $parameters = array(1 => $user_id);
        $team_money = $this :: SUM(array(
            $conditions,
            "column" => "money+money_plus",
            "bind" => $parameters
        ));

        return [
            'this_money' => $this_money?$this_money:0,
            'team_money' => $team_money?$team_money:0
        ];
    }


    //粉丝详情
    public function getOrderCommissionCount($user_id,$start_end_time){
        $conditions = "user_id in(?1) AND tk_create_time >= ?2 AND tk_create_time < ?3 AND order_status in (2,4,6)";
        $parameters = array(1 => $user_id,2 => $start_end_time['start_at'],3 => $start_end_time['end_at']);
        $money_array = $this :: find(array(
        $conditions,
        "columns" => "money+money_plus as money ,commission_type",
        "bind" => $parameters
        ));
        $newList = array();
        if(!empty($money_array)){
            foreach ($money_array as $value){
                $newList[$value->commission_type] += $value->money;
            }
        }

        return $newList;
    }

    /**
     * 计算结算预估佣金
     * @param $user_id 用户id
     * @param $start_end_time 开始时间和结束时间
     * @param $site 所属平台 taobao pinduoduo jd 如果没有，则查找三个平台的总和
     */
    public function getOrderSettlementCommission($user_id,$start_end_time,$site=""){
        if($site){
            $conditions = "user_id = ?1 AND tk_earning_time >= ?2 AND tk_earning_time < ?3 AND site = ?4 AND order_status = 6";
            $parameters = array(1 => $user_id,2 => $start_end_time['start_at'],3 => $start_end_time['end_at'],4=>$site);
        }else{
            $conditions = "user_id = ?1 AND tk_earning_time >= ?2 AND tk_earning_time < ?3 AND order_status = 6";
            $parameters = array(1 => $user_id,2 => $start_end_time['start_at'],3 => $start_end_time['end_at']);
        }
        $money_array = $this :: SUM(array(
            $conditions,
            "column" => "money+money_plus",
            "bind" => $parameters
        ));
        return $money_array ? $money_array : 0;
    }




    /**
     * 计算结算预估佣金
     * @param $user_id 用户id
     * @param $start_end_time 开始时间和结束时间
     * @param $site 所属平台 taobao pinduoduo jd 如果没有，则查找三个平台的总和
     */
    public function getOrderSettlementCommission1($user_id, $start_end_time, $site = "")
    {
        if ($site) {
            $conditions = "user_id = ?1 AND tk_earning_time >= ?2 AND tk_earning_time < ?3 AND site = ?4 AND order_status in(4,6)";
            $parameters = array(1 => $user_id, 2 => $start_end_time['start_at'], 3 => $start_end_time['end_at'], 4 => $site);
        } else {
            $conditions = "user_id = ?1 AND tk_earning_time >= ?2 AND tk_earning_time < ?3  AND order_status in(4,6)";
            $parameters = array(1 => $user_id, 2 => $start_end_time['start_at'], 3 => $start_end_time['end_at']);
        }
        $money_array = $this:: SUM(array(
            $conditions,
            "column" => "money+money_plus",
            "bind"   => $parameters
        ));
        return $money_array ? $money_array : 0;
    }



    /**
     * 计算结算预估佣金
     * @param $user_id 用户id
     * @param $start_end_time 开始时间和结束时间
     * @param $site 所属平台 taobao pinduoduo jd 如果没有，则查找三个平台的总和
     */
    public function getOrderSettlementCommission_new3($user_id,$start_end_time,$site=""){
        if($site){
            $conditions = "user_id = ?1 AND tk_create_time >= ?2 AND tk_create_time < ?3 AND site = ?4 AND order_status in(2,4,6)";
            $parameters = array(1 => $user_id,2 => $start_end_time['start_at'],3 => $start_end_time['end_at'],4=>$site);
        }else{
            $conditions = "user_id = ?1 AND tk_create_time >= ?2 AND tk_create_time < ?3 AND order_status in(2,4,6)";
            $parameters = array(1 => $user_id,2 => $start_end_time['start_at'],3 => $start_end_time['end_at']);
        }
        $money_array = $this :: SUM(array(
            $conditions,
            "column" => "money+money_plus",
            "bind" => $parameters
        ));
        return $money_array ? $money_array : 0;
    }


    /*
     添加用户分账订单
     */
	public function add_commissions($order,$money){
        $path = BASE_PATH."plugins/Jpush.php";
        require_once $path;
        $push = new \JPush($this->di->getConfig()->services->union->tbUnion->toarray());

		$condition['conditions']="order_id = '{$order['order_id']}'";
		$res=$this->find($condition);
		if($res->toArray() != []){
			foreach($res as $k =>$v){
			$v->delete();
			}
		}
		foreach($money as $k =>$v){

			if($v['number'] == 1 && isset($v['percent']) && isset($v['id'])){
                $res=new OrderCommissions();
				$res->order_id=$order['order_id'];
				$res->user_id=$v['id'];
				$res->money=substr(sprintf("%.4f", $v['percent']), 0, -2);
				if(isset($v['money_plus'])){
				$v['money_plus']=$res->money_plus=$v['money_plus'];
				}else{
					$v['money_plus']=0;
				}
				$res->buyer_id=$order['user_id'];
				$res->commission_type=$v['id']==$order['user_id']?1:2;
				$res->order_status=$order['order_status'];
				$res->tk_create_time=$order['order_create_time'];
				$res->tk_paid_time=$order['order_paid_time'];
				$res->tk_earning_time=$order['order_earning_time'];
				$res->site=$order['site'];
				$res->settlement_type=0;
				$res->updated_at=$res->created_at=date('Y-m-d H:i:s');
				$res->level_type=$k;
				if(isset($v['is_free'])){
				$res->is_free=1;
				}
				if($v['id'] != $order['user_id'] && $v['percent'] == 0){
				continue;
				}
				$res->save();

				if($v['percent'] != "0.00" && $order['order_status'] == 2 && isset($v['money']) && $v['money'] > 0) {
                    $alias = [$v['id']];
                    $title = '巨折新订单';
                    //$detail = '恭喜！获得订单佣金' . ($v['percent']+$v['money_plus']) . '元已存入钱包';
                    //$detail = '恭喜,你有新收入了！订单编号：'.$order['order_id'];
                    $commission_money = $v['money']+$v['money_plus'];
                    $detail = "亲！您有一笔淘宝订单预计订单收益{$commission_money}元，点击查看详情";
                    $route = [
                        'type' => 1,
                        'path' => 'AwakeOrderDetail',
                        'title' => '路由标题',
                        'url' => '',
                        'data' => '',
                        'notice' => null,
                    ];
                    try {
                        //设置-消息推送 2020 3 31
                        $user = new User();
                        $user_infos = $user->find_by_id($order['user_id']);
                        if($user_infos->is_news == 1) {
                            $push->pushmsg($alias, $title, $detail, $route);

                            $message = new Messages();
                            $tips="您有新的订单，订单编号:{$order['order_id']}";
                            $message->save_params($order['user_id'],$tips,$title);

                        }
                    } catch (Exception $e) {

                    }
                }
			}
		}
    }

	//修改佣金
	public function update_commissions($order,$money){
		foreach($money as $k =>$v){

			if($v['number'] == 1){
		$condition['conditions']="order_id = '{$order['order_id']}'";
		$res=$this->find($condition);
		if($res->toArray() != []){
			foreach($res as $k =>$v){
			$v->delete();
			}
		}
							if(isset($v['money_plus'])){
				$res->money_plus=$v['money_plus'];
				}
				$res->money=$v['percent'];
				$res->order_status=$order['order_status'];
				$res->tk_create_time=$order['order_create_time'];
				$res->tk_paid_time=$order['order_paid_time'];
				$res->tk_earning_time=$order['order_earning_time'];
				$res->updated_at=date('Y-m-d H:i:s');
				$res->level_type=$k;
				$res->save();
			}
		}
	
	}




    /**
     * @func 根据用户时间获取各个平台的数据总和 自购订单数量
     * @param $user_id 用户id
     * @param $start_end_time 开始时间和结束时间
     * @param $site 所属平台 taobao pinduoduo jd 如果没有，则查找三个平台的总和
     * @return array 以平台名称作为键 统计作为结果
     */
    public function getOrderCount($user_id,$start_end_time,$site=''){
        if($site){
            $conditions = "user_id = ?1 AND tk_create_time >= ?2 AND tk_create_time < ?3 AND site = ?4 and commission_type=1 and order_status in (2,4,6)";
            $parameters = array(1 => $user_id,2 => $start_end_time['start_at'],3 => $start_end_time['end_at'],4=>$site);
        }else{
            $conditions = "user_id = ?1 AND tk_create_time >= ?2 AND tk_create_time < ?3 and commission_type=1 and order_status in (2,4,6)";
            $parameters = array(1 => $user_id,2 => $start_end_time['start_at'],3 => $start_end_time['end_at']);
        }

        $count_array = $this::count(array(
            $conditions,
            "columns" => "*",
            "group" => "site",
            "bind" => $parameters
        ));

        $new_array = array();
        foreach($count_array as $key=>$value){
            $new_array[$value['site']] = $value['rowcount'];//site
        }
        return $new_array;
    }
	public function get_up_money($user_id,$day=null){
		$condition['conditions']="user_id =$user_id and order_status in (2,4,6) and commission_type=2 ";
		if($day){
			$condition['conditions'] .= "and tk_paid_time BETWEEN '".date("Y-m-d H:i:s",strtotime("-".$day." day"))."' and '".date('Y-m-d H:i:s')."'  ";
		}
		$condition['columns']="sum(money+money_plus) as money";
		$res=$this->findFirst($condition);
		return doubleval($res->money);
	}

	/*
	 批量计算市场补贴
	 */
	public function calculate_market($start_end_time){
		$condition['conditions']="user_id != 1005 and tk_create_time >= '{$start_end_time['start_at']}'  and tk_create_time <= '{$start_end_time['end_at']}'";
		$condition['columns']='sum(money) + sum(money_plus) as money,user_id';
		$condition['group']='user_id';
		$res=$this->find($condition)->toArray();
		return $res;
	}

    /**
     * 佣金结算
     */
	public function  get_month_user($start_end_time,$limit=500){
        $condition=[
            'conditions'=>"order_status IN (4, 6) and  tk_earning_time BETWEEN '".$start_end_time['start_at']."' AND '".$start_end_time['end_at']."' AND settlement_type=0",
                'columns'=>'id,user_id, SUM(money + money_plus) AS money',
        		'limit'=>$limit,
                'group'=>'id',
            ];
        $res=$this->find($condition)->toArray();
        return $res;
    }
	public function get_free_order($user_id){
		$condition=["conditions"=>"user_id = $user_id and is_free =1","columns"=>'id'];
		$res['free']=$this->count($condition);
		$condition=["conditions"=>"user_id = $user_id and is_free = 0 and commission_type= 1" ];
		$res['all']=$this->count($condition);
		$condition=["conditions"=>"user_id = $user_id and is_free =1","columns"=>'order_id','order'=>'id asc'];
		$a=$this->findFirst($condition);
		if($a != false){
		$res['order_id']=$a->order_id;
		}else{
			$res['order_id']='';
		}
		
		return $res;
	}

	//获取粉丝对我的佣金贡献
	public function get_fans_info($myid,$fans_id,$time){
		$data['all_order_count']=0;
		$data['near_order_time']='';
		
		$conditions['conditions']="buyer_id = $fans_id and commission_type=1";
		$conditions['columns']='sum(money+money_plus) as c';
		$res=$this->findFirst($conditions);
		if($res){
		$data['all_order_count']=$res->c."";
		}
		
		$conditions['columns']='count(id) as c';
		$conditions['conditions']="buyer_id = $fans_id and commission_type=1 and tk_create_time BETWEEN '{$time['week']['start_at']}' and '{$time['week']['end_at']}' ";

		$res=$this->findFirst($conditions);
		if($res){
		$data['week_order_count']=$res->c."";
		}
		$conditions['conditions']="user_id = $myid and commission_type=2 and buyer_id = $fans_id and tk_create_time BETWEEN '{$time['month']['start_at']}' and '{$time['month']['end_at']}' ";
		$conditions['columns']='SUM(money+money_plus) as c';

		$res=$this->findFirst($conditions);
		if($res){
		$data['month_money_for_me']=doubleval($res->c)."";
		}
		$conditions['conditions']="buyer_id = $fans_id and commission_type=1";
		$conditions['columns']='tk_create_time';
		$conditions['order']='id desc';
		$res=$this->findFirst($conditions);
		if($res){
		$data['near_order_time']=$res->tk_create_time;
		}
		return $data;

	}


    /**
     * 2020/3/23
     * 运营商升级条件
     * @param $user_id
     * @param $start_end_time
     * @return bool
     */
    public function get_income_month($user_id,$start_end_time){
        $res['zi_money'] = 0;
        $res['fen_money'] = 0;
        $condition['conditions']="user_id = $user_id and tk_earning_time >= '{$start_end_time['start_at']}'  and tk_earning_time <= '{$start_end_time['end_at']}' and commission_type =2 and settlement_type = 1  ";
        $condition['columns']='sum(money) + sum(money_plus) as money,user_id';
        $fen_res = $this->findfirst($condition);
        if($fen_res){
            $res['fen_money'] = $fen_res->money;
        }
        $conditions['conditions']="user_id = $user_id and tk_earning_time >= '{$start_end_time['start_at']}'  and tk_earning_time <= '{$start_end_time['end_at']}' and commission_type =1  and settlement_type = 1 ";
        $conditions['columns']='sum(money) + sum(money_plus) as money,user_id';
        $zi_res = $this->findfirst($conditions);
        if($zi_res){
            $res['zi_money'] = $zi_res->money >= 80?80:$zi_res->money;
        }else{
            $res['zi_money'] = 0;
        }

        return $res;
    }

    /**
     * 2020/3/23
     * 高级运营商升级条件
     * @param $user_id
     * @param $start_end_time
     * @return bool
     */
    public function get_senior_income($user_id){
        $res['zi_money'] = 0;
        $res['fen_money'] = 0;
        $condition['conditions']="user_id = $user_id  and commission_type =2  and settlement_type = 1 ";
        $condition['columns']='sum(money) + sum(money_plus) as money,user_id';
        $fen_res = $this->findfirst($condition);
        if($fen_res){
            $res['fen_money'] = $fen_res->money;
        }
        $conditions['conditions']="user_id = $user_id  and commission_type =1  and settlement_type = 1 ";
        $conditions['columns']='sum(money) + sum(money_plus) as money,user_id';
        $zi_res = $this->findfirst($conditions);
        if($zi_res){
            $res['zi_money'] = $zi_res->money >= 10000?10000:$zi_res->money;
        }

        return $res;
    }



	/**
	 * 团队订单和个人订单结算统计
	 * @param $user_id 用户id
	 * @param $start_end_time 开始时间和结束时间
	 * @param $site 所属平台 taobao pinduoduo jd 如果没有，则查找三个平台的总和
	 */
	public function getOrderCommissions_tj($user_id,$start_end_time='',$data=[]){

		if(!empty($start_end_time)){
			$conditions = "user_id={$user_id} AND tk_create_time >= '{$start_end_time['start_at']}' AND tk_create_time < '{$start_end_time['end_at']}'";        //预估收入
		}else{
			$conditions = "user_id={$user_id}";
		}


		if(isset($data['order_status']) && !empty($data['order_status'])){
			$conditions .= " AND order_status in({$data['order_status']})";
		}
		if(isset($data['buyer_id']) && !empty($data['buyer_id'])){
			$conditions .= " AND buyer_id in({$data['buyer_id']})";
		}
        if(isset($data['nobuyer_id']) && !empty($data['nobuyer_id'])){
            $conditions .= " AND buyer_id<>{$data['nobuyer_id']}";
        }
        if(isset($data['site']) && !empty($data['site'])){
		    $conditions .= " AND site='{$data['site']}'";
        }
        $money_array = $this :: SUM(array(
			$conditions,
			"column" => "money+money_plus",
		));
		return $money_array ? $money_array : 0;
	}




    /**
     * 团队订单和个人订单结算统计
     * @param $user_id 用户id
     * @param $start_end_time 开始时间和结束时间
     * @param $site 所属平台 taobao pinduoduo jd 如果没有，则查找三个平台的总和
     */
    public function getOrderCommissions_earning_time_tj($user_id,$start_end_time='',$data=[]){

        if(!empty($start_end_time)){
            $conditions = "user_id={$user_id} AND tk_earning_time >= '{$start_end_time['start_at']}' AND tk_earning_time < '{$start_end_time['end_at']}'";        //预估收入
        }else{
            $conditions = "user_id={$user_id}";
        }


        if(isset($data['order_status']) && !empty($data['order_status'])){
            $conditions .= " AND order_status in({$data['order_status']})";
        }
        if(isset($data['buyer_id']) && !empty($data['buyer_id'])){
            $conditions .= " AND buyer_id in({$data['buyer_id']})";
        }
        if(isset($data['nobuyer_id']) && !empty($data['nobuyer_id'])){
            $conditions .= " AND buyer_id<>{$data['nobuyer_id']}";
        }
        if(isset($data['site']) && !empty($data['site'])){
            $conditions .= " AND site='{$data['site']}'";
        }
        $money_array = $this :: SUM(array(
            $conditions,
            "column" => "money+money_plus",
        ));
        return $money_array ? $money_array : 0;
    }


    //总订单
    public function order_all_estimate($user_id,$start_end_time=0){
        if($start_end_time){
            $condition=[
                "conditions" => "user_id={$user_id} and order_status IN (2,4,6)  AND tk_create_time >= '{$start_end_time['start_at']}' AND tk_create_time < '{$start_end_time['end_at']}'",
                "columns" => "id"
            ];
        }else{
            $condition=[
                "conditions" => 'user_id='.$user_id.' and order_status IN (2,4,6) ',
                "columns" => "id"
            ];
        }
        $res=$this->count($condition);
        return $res.'';

    }


    /**
     * 保存数据
     */
    public function save_comm($h_params,$current_order,$status){
        $conditions = "order_id = '{$h_params['order_sn']}'";
        $find_list = $this:: find(array(
            $conditions,
        ));
        foreach($find_list as $key=>$value){
            $value->order_status=$status;
            $value->tk_create_time=$current_order->order_create_time;
            $value->tk_paid_time=$current_order->order_paid_time;
            $value->tk_earning_time=$current_order->order_earning_time;
            $value->save();
        }
    }



}


